package oa.service.impl;

import com.alibaba.fastjson.JSON;
import oa.bean.*;
import oa.dao.DeptMapper;
import oa.dao.QxcdMapper;
import oa.dao.QxjsMapper;
import oa.dao.UserMapper;
import oa.service.UserService;
import oa.util.StringUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component("userService")
//@Service(interfaceClass=UserService.class)
@CacheConfig(cacheNames = {"myCache"})
public class UserServiceImpl implements UserService {

	@Resource
	private UserMapper userMapper;
	@Resource
	private DeptMapper deptMapper;
	@Resource
	private QxcdMapper qxcdMapper;
	@Resource
	private QxjsMapper qxjsMapper;

	@Override
	public TUser login(String username) {
		return userMapper.login(username);
	}

	@Override
	public List<QXCD> listmenu1(String jsdm) {
		return qxcdMapper.listmenu1(jsdm);
	}

	@Override
	public List<QXCD> listmenu2(String jsdm) {
		return qxcdMapper.listmenu2(jsdm);
	}
	
	@Override
	public List<QXJS> listrole() {
		return qxjsMapper.listrole();
	}

	@Override
	public String listjsfortree(String jsdm, String flag) {
		List<QXCD> list = null;
		List<QXCD> list2 = null;
		//获取角色下所有的菜单
		if(!StringUtil.nullOrBlank(flag)){
			//显示权限
			list = qxcdMapper.listmenu1(jsdm);
		}else{
			//设置权限
			list = qxcdMapper.listmenu();
			list2 = qxcdMapper.listmenu1(jsdm);
		}
		//根据菜单列表拼 json 字符串
		StringBuffer tree = new StringBuffer("[");
		for (QXCD cd : list) {
			tree.append("{\"id\":\"" + cd.getCddm()+"\"");//菜单代码
			tree.append(",\"text\":\"" + cd.getCdmc() + "\"");//菜单名称
			tree.append(",\"attributes\":{ \"jscd\":true}");//菜单自定义属性
            //获取当前菜单的所有的子菜单
            //结构：[{id:1,text:'权限管理'},{...}]
			String children = listcdfortree(jsdm,cd.getCddm(),flag);
			if(!"[]".equals(children)){//如果存在子菜单
				tree.append(",\"state\":\"closed\"");
				tree.append(",\"children\":").append(children);
			}else{
				if(list2!=null){//选中权限菜单
					for(QXCD c : list2){
						if(c.getCddm().equals(cd.getCddm()))
							tree.append(",\"checked\":true");
					}
				}
			}
			tree.append("},");
		}
		if(list.size()>0){//删除最后一个逗号
			tree.deleteCharAt(tree.length()-1);
		}
		tree.append("]");
		//生成json字符串
    	return tree.toString();
	}

	@Override
	public String listcdfortree(String jsdm, String sjcd, String flag) {
		List<QXCD> list = null;
		List<QXCD> list2 = null;
		if(!StringUtil.nullOrBlank(flag)){
			list = qxcdMapper.listsubmenu1(jsdm, sjcd);
		}else{
			list = qxcdMapper.listsubmenu(sjcd);
			list2 = qxcdMapper.listsubmenu1(jsdm, sjcd);
		}
		StringBuffer tree = new StringBuffer("[");
		for (QXCD cd : list) {
			tree.append("{\"id\":\"" + cd.getCddm()+"\"");
			tree.append(",\"text\":\"" + cd.getCdmc() + "\"");
			tree.append(",\"attributes\":{ \"jscd\":true}");

			if(list2!=null){
				if(list2!=null){//选中权限菜单
					for(QXCD c : list2){
						if(c.getCddm().equals(cd.getCddm()))
							tree.append(",\"checked\":true");
					}
				}
			}
			tree.append("},");
		}
		if(list.size()>0){
			tree.deleteCharAt(tree.length()-1);
		}
		tree.append("]");
		return tree.toString();
	}

	@Override
	public QXJS getrole(String jsdm) {
		return qxjsMapper.getrole(jsdm);
	}

	@Override
	public int delrole(String jsdm) {
		return qxjsMapper.delrole(jsdm);
	}

	@Override
	public int addrole(QXJS qxjs) {
		return qxjsMapper.addrole(qxjs);
	}

	@Override
	public int addrolecd(String jsdm, String cddm) {
		return qxjsMapper.addrolecd(jsdm, cddm);
	}

	@Override
	public int updaterole(QXJS qxjs) {
		try {
			return qxjsMapper.updaterole(qxjs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int updaterolezt(String jsdm, int zt) {
		return qxjsMapper.updaterolezt(jsdm, zt);
	}

	@Override
	public int delrolecd(String jsdm) {
		return qxjsMapper.delrolecd(jsdm);
	}

	@Override
	@Transactional
	public int updaterolecd(String jsdm, String[] cddms) throws Exception {
		int i = 0;
		try {
			qxjsMapper.delrolecd(jsdm);
			for(String cddm:cddms) {
				qxjsMapper.addrolecd(jsdm, cddm);
			}
			i = 1;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return i;
	}

	@Override
	public List<String> getQxcd(String username) {
		return userMapper.getQxcd(username);
	}

	@Override
	public List<TUser> listuser(String username, String fullname, String oskey, Integer deptid) {
		return userMapper.listuser(username, fullname, oskey, deptid);
	}

	@Override
	public TUser getuser(int userid) {
		return userMapper.getuser(userid);
	}

	@Override
	public int adduser(TUser user) {
		user.setUserpassword(StringUtil.MD5(user.getUserpassword()));
		return userMapper.adduser(user);
	}

	@Override
	public int updateuser(TUser user) {
		TUser obj = getuser(user.getUserid());
		String pass = user.getUserpassword();
		if(obj.getUserpassword().equals(pass)) {
			user.setUserpassword(null);
		} else {
			user.setUserpassword(StringUtil.MD5(pass));
		}
		return userMapper.updateuser(user);
	}

	@Override
	public int deluser(int userid) {
		return userMapper.deluser(userid);
	}

	@Override
	public int updateuserzt(int userid, int zt) {
		return userMapper.updateuserzt(userid, zt);
	}

	@Override
	public int updateuserlogin(String username, String lastlogin) {
		return userMapper.updateuserlogin(username, lastlogin);
	}

	@Override
	public List<TDep> listdept() {
		return deptMapper.listdept();
	}

	@Override
	public TDep getdept(int deptid) {
		return deptMapper.getdept(deptid);
	}

	@Override
	public int adddept(TDep dept) {
		return deptMapper.adddept(dept);
	}

	@Override
	public int updatedept(TDep dept) {
		return deptMapper.updatedept(dept);
	}

	@Override
	public int deldept(int deptid) {
		return deptMapper.deldept(deptid);
	}

	@Override
	public int updatedeptzt(int deptid, int zt) {
		return deptMapper.updatedeptzt(deptid, zt);
	}

	@Override
	public String listdeptfortree() {
		// 声明一个部门列表
		List<Tree> treeList = new ArrayList<Tree>();
		// 虚拟一个最顶级的机构——组织机构
		Tree tree = new Tree();
		tree.setId("0");
		tree.setText("组织机构");
		tree.setIconCls("icon-role");
		// 从数据库获取部门 ID=0 的部门列表，即一级部门——星空互联
		List<TDep> list = deptMapper.subdept(0);
		// 调用 treeList(List<TDep>) 将部门以下的所有部门全部增加进来
		tree.setChildren(treeList(list));
		// 虚拟的机构加到部门列表
		treeList.add(tree);
		// 将部门列表转化为 JSON
		return JSON.toJSONString(treeList);
	}

	// 递归
	public List<Tree> treeList(List<TDep> list){
		List<Tree> treeList = new ArrayList<Tree>();
		for (TDep dep : list) {
			Tree tree = new Tree();
			tree.setId(dep.getId()+"");
			tree.setText(dep.getName());
			//部门下的所有部门
			List<TDep> subdep = deptMapper.subdept(dep.getId());
			//如果该部门存在下级部门
			if(subdep.size()>0){
				//继续执行 treeList 方法
				tree.setChildren(treeList(subdep));
			}
			treeList.add(tree);
		}
		return treeList;
	}

	@Override
	public TSystem getsystem() {
		return userMapper.getsystem();
	}

	@Override
	public int updatesystem(TSystem sys) {
		return userMapper.updatesystem(sys);
	}
}
